//
//  visionController.m
//  BuffetNews
//
//  Created by weidawei on 14/11/7.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "visionController.h"
#import "VisionDome.h"
#import "NetHandler.h"
#import "UIImageView+WebCache.h"
#import "VisionCollectionCell.h"
#import "UIColor+myColor.h"
#import "NightModel.h"
#import "WebColor.h"
#import "Reachability.h"
#import <SystemConfiguration/SystemConfiguration.h>

@interface visionController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView * collection;
@property (nonatomic, retain) NSMutableArray * imageArr;
@property (nonatomic, retain) NSMutableArray * arrBigImage;
@property (nonatomic, retain) NSString * pic;
@property (nonatomic, retain) NSString * block_color;
@end

@implementation visionController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.imageArr = [NSMutableArray array];
        self.arrBigImage = [NSMutableArray array];
    }
    return self;
}
- (void)dealloc
{
    [_collection release];
    [_imageArr release];
    [_arrBigImage release];
    [_pic release];
    [_block_color release];
    [_visionApi release];
    [super dealloc];
}

- (void)loadingData
{
    BOOL reachable = [[Reachability reachabilityForInternetConnection] isReachable];
    
    if (!reachable) {
        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(5, self.view.frame.size.height / 13 * 12, self.view.frame.size.width / 7, self.view.frame.size.height / 14 );
        [button setBackgroundImage:[UIImage imageNamed:@"btn_home.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(block:) forControlEvents:UIControlEventTouchUpInside];;
        [self.view addSubview:button];
        
        UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"请检查您的网络连接状态" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] autorelease];
        [alertView show];
        
        
        
    } else {
    
    [NetHandler networkWithURL:self.visionApi comaletion:^(id result) {
        VisionDome * vision = [[VisionDome alloc] init];
        NSDictionary * dic  = result;
        NSDictionary * dic1 = [dic objectForKey:@"data"];
        NSDictionary * dic2 = [dic1 objectForKey:@"ipadconfig"];
        NSMutableArray * arr = [NSMutableArray array];
        NSMutableArray * arrArticles = [NSMutableArray array];
        NSMutableArray * arrTpl_group = [NSMutableArray array];
        NSDictionary * dic4 = [dic1 objectForKey:@"block_info"];
        self.pic = [dic4 objectForKey:@"pic"];
        self.block_color = [dic4 objectForKey:@"block_color"];
        arr = [dic2 objectForKey:@"pages"];
        for (NSDictionary * dic3 in arr) {
            
            [arrArticles addObject:[[dic3 objectForKey:@"articles"] componentsSeparatedByString:@","]];
            
            [arrTpl_group addObject:[dic3 objectForKey:@"tpl_group"]];
        }
        NSMutableArray * arr1 = [NSMutableArray array];
        NSMutableArray * arr2 = [NSMutableArray array];
        NSMutableArray * arr3 = [NSMutableArray array];
        NSMutableArray * arr4 = [NSMutableArray array];
        NSMutableArray * arr5 = [NSMutableArray array];
        NSMutableArray * arr6 = [NSMutableArray array];
        
        NSMutableArray * arr11 = [NSMutableArray array];
        NSMutableArray * arr22 = [NSMutableArray array];
        NSMutableArray * arr33 = [NSMutableArray array];
        NSMutableArray * arr44 = [NSMutableArray array];
        NSMutableArray * arr55 = [NSMutableArray array];
        NSMutableArray * arr66 = [NSMutableArray array];
        
        NSMutableArray * articlesArr = [NSMutableArray array];
        articlesArr = [dic1 objectForKey:@"articles"];
            NSLog(@"*****************//////////////%d",articlesArr.count);
        if ([arrArticles count] == 1) {
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[0]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr1 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr11 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr11];
        }
        
        
        else if ([arrArticles count] == 2) {
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[0]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr1 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr11 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr11];
            
            vision.m_urlImageString = nil;
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[1]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr2 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr22 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr22];
            
        } else if ([arrArticles count] == 3) {
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[0]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr1 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr11 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr11];
            
            vision.m_urlImageString = nil;
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[1]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr2 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr22 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr22];
            vision.m_urlImageString = nil;
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[2]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr3 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr33 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr33];

            
            
        } else if ([arrArticles count] == 4) {
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[0]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr1 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr11 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr11];
            
            vision.m_urlImageString = nil;
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[1]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr2 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr22 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr22];
            vision.m_urlImageString = nil;
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[2]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr3 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr33 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr33];
            
            vision.m_urlImageString = nil;
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[3]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr4 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr44 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr44];

            
            
        } else if ([arrArticles count] == 5) {
            
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[0]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr1 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr11 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr11];
            
            vision.m_urlImageString = nil;
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[1]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr2 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr22 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr22];
            vision.m_urlImageString = nil;
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[2]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr3 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr33 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr33];
            
            vision.m_urlImageString = nil;
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[3]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr4 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr44 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr44];
            
            vision.m_urlImageString = nil;
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[4]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr5 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr55 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr55];

            
            
            
        }else if ([arrArticles count] == 6) {
            
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[0]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr1 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr11 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr11];
            
            vision.m_urlImageString = nil;
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[1]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr2 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr22 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr22];
            vision.m_urlImageString = nil;
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[2]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr3 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr33 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr33];
            
            vision.m_urlImageString = nil;
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[3]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr4 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr44 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr44];
            
            vision.m_urlImageString = nil;
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[4]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr5 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr55 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr55];
            vision.m_urlImageString = nil;
            for (NSDictionary * dica in articlesArr) {
                for (NSString * string in arrArticles[5]) {
                    if ([string isEqualToString:[dica objectForKey:@"pk"]]) {
                        [arr6 addObject:dica];
                        vision.m_urlImageString = [[dica objectForKey:@"media"][0] objectForKey:@"m_url"] ;
                        [arr66 addObject:vision.m_urlImageString];
                    }
                }
            }
            [self.imageArr addObject:arr66];

            
            
        }
         [self stataView];
         [self.collection reloadData];
        
    }];
    }
    
}
-(BOOL)isReachableViaWWAN
{
    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}
- (void)stataView
{
    UICollectionViewFlowLayout * flowlayout = [[UICollectionViewFlowLayout alloc] init];
    flowlayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //cell之间的间距
    flowlayout.minimumInteritemSpacing = 0;
    //cell行之间的间距
    flowlayout.minimumLineSpacing = 0;
    flowlayout.sectionInset = UIEdgeInsetsMake(1, 0, 1, 1);
    self.collection  = [[UICollectionView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 20, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowlayout];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.pagingEnabled = YES;
    
    if ([NightModel shareInstance].open == 1) {
        self.collection.backgroundColor = [WebColor dimGray];
    } else {
        self.collection.backgroundColor = [UIColor whiteColor];
    }

    
    
    
    [self.collection  registerClass:[VisionCollectionCell class] forCellWithReuseIdentifier:@"1"];
    self.collection.backgroundColor = [UIColor whiteColor];
    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(5, self.view.frame.size.height / 13 * 12, self.view.frame.size.width / 7, self.view.frame.size.height / 14 );
    [button setBackgroundImage:[UIImage imageNamed:@"btn_home.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(block:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.collection];
    [self.view addSubview:button];
    [_collection release];
    
   
}


- (void)block:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadingData];

    
    
    
    // Do any additional setup after loading the view.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    VisionCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"1" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor cyanColor];
//    VisionDome * visrs = self.imageArr[indexPath.item][indexPath.item];
    if (indexPath.item == self.imageArr.count - 2 || indexPath.item == self.imageArr.count - 1 || indexPath.item == self.imageArr.count - 3 || indexPath.item == self.imageArr.count - 4 || indexPath.item == self.imageArr.count  - 5) {
       
    
    if ([self.imageArr [indexPath.item] count] == 7) {
        [cell.imageAisionOne sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][0]]];
        [cell.imageAisionTow sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][1]]];
        [cell.imageAisionThree sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][2]]];
        [cell.imageAisionFour sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][3]]];
        [cell.imageAisionFive sd_setImageWithURL:[NSURL URLWithString:self.pic]];
        cell.imageAisionFive.backgroundColor = [UIColor colorWithHexString:self.block_color];
        [cell.imageAisionSix sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][4]]];
        [cell.imageAisionSeven sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][5]]];
        [cell.imageAisionEight sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][6]]];

    } else {
        [cell.imageAisionOne sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][0]]];
        [cell.imageAisionTow sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][1]]];
        [cell.imageAisionThree sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][2]]];
        [cell.imageAisionFour sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][3]]];
        [cell.imageAisionFive sd_setImageWithURL:[NSURL URLWithString:self.pic]];
        cell.imageAisionFive.backgroundColor = [UIColor colorWithHexString:self.block_color];
        [cell.imageAisionSix sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][4]]];
        [cell.imageAisionSeven sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][5]]];
        cell.imageAisionEight.image = [UIImage imageNamed:@"6.jpg"];
//            [cell.imageAisionEight sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][6]]];
    }
    } else {
        [cell.imageAisionOne sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][0]]];
        [cell.imageAisionTow sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][1]]];
        [cell.imageAisionThree sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][2]]];
        [cell.imageAisionFour sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][3]]];
        [cell.imageAisionFive sd_setImageWithURL:[NSURL URLWithString:self.pic]];
        cell.imageAisionFive.backgroundColor = [UIColor colorWithHexString:self.block_color];
        [cell.imageAisionSix sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][4]]];
        [cell.imageAisionSeven sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][5]]];
        [cell.imageAisionEight sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.item][6]]];
    }
    
    if ([NightModel shareInstance].open == 1) {
        cell.backgroundColor = [WebColor dimGray];
        self.collection.backgroundColor = [WebColor dimGray];
    } else {
        cell.backgroundColor = [UIColor whiteColor];
        self.collection.backgroundColor = [UIColor whiteColor];
    }

    


    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
